from flask import Flask, render_template, url_for, request, redirect
import cv2

app = Flask(__name__)

img = cv2.imread('static/images/image1.jpg', 1)
face_engine = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
faces = face_engine.detectMultiScale(img, scaleFactor=1.3, minNeighbors=5)
for (x, y, w, h) in faces:
        img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('static/images/output.jpg', img)

def return_img_stream(img_local_path):
    import base64
    img_stream = ''
    with open(img_local_path, 'rb') as img_f:
        img_stream = img_f.read()
        img_stream = base64.b64encode(img_stream)
    return img_stream
@app.route('/')
def hello_world():
    return 'hello,world'

@app.route('/index')
def show_index():
    return render_template('index.html')
@app.route('/show')
def hello():
    img_path = 'static/images/output.jpg'
    img_stream = return_img_stream(img_path).decode()
    return render_template('result.html',img_stream=img_stream)
if __name__ == '__main__':
    app.run(debug=True)
